package com.app.freshmenapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

    private Button mlogin;
    private EditText User;
    private EditText Password;
    Boolean success;
    private String user;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initEvent();

    }

    private void initView() {
        mlogin = findViewById(R.id.btn_login);
        User = findViewById(R.id.edt_user);
        Password = findViewById(R.id.edt_password);
    }

    private void initEvent() {
        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

    }

    private void login() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);//创建请求队列

        user = User.getText().toString();
        password = Password.getText().toString();

        final String url = "http://10.1.65.40:8083/freshmenapp/user/login";
        JSONObject object = new JSONObject();
        try {
            object.put("name", user);
            object.put("password", password);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //创建请求
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST,
                url,
                object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            success=jsonObject.getBoolean("success");
                            if (success==true){
                                Toast.makeText(LoginActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(LoginActivity.this, "失败", Toast.LENGTH_SHORT).show();
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        //将请求加入队列
        requestQueue.add(jsonObjectRequest);
    }
}
